package org.yjc.controller;

import java.util.List;

import javax.swing.plaf.synth.SynthSeparatorUI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.entity.Course;
import org.yjc.entity.Grade;
import org.yjc.entity.Student;
import org.yjc.entity.TbClass;
import org.yjc.service.CourseService;
import org.yjc.service.GradeService;
import org.yjc.service.StudentService;
import org.yjc.service.TbClassService;

@Controller
@RequestMapping(value="/grade")
public class GradeController {
	@Autowired
	private GradeService gradeservice;
	@Autowired
	private CourseService courseservice;
	@Autowired
	private StudentService studentservice;
	@Autowired
	private TbClassService tbclassservice;
	
	@RequestMapping(value="/addgrade")
	public String addgrade(ModelMap map) {
		List<TbClass> tbClass = tbclassservice.findAll();
		List<Course> course = courseservice.findAll();
		map.put("tbClass", tbClass);
		map.put("course", course);
		return "grade/addgrade";
	}
	
	@RequestMapping(value="/allstudent")
	public String xs_all(Integer tbclassid, ModelMap map,Integer courseid) {
		List<Student> student = studentservice.findByTbClassId(tbclassid);
		Course course = courseservice.findById(courseid);
		List<Grade> grade = gradeservice.findByStudentTbClassIdAndCourseId(tbclassid, courseid);
		for (Student s : student) {
			for (Grade g : grade) {
				if((course.getId() == g.getCourse().getId()) && (s.getId() == g.getStudent().getId())) {
					s.setCj(g.getGrade());
					break;
				}
			}
		}
		map.put("student", student);
		return "grade/allstudent";

	}
	
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@ResponseBody
	public void save(Integer grade, Integer kcid, Integer xsid) {
		Grade g = gradeservice.findByStudentIdAndCourseId(xsid, kcid);
		Grade cj = new Grade();
		cj.setGrade(grade);
		cj.setCourse(courseservice.findById(kcid));
		cj.setStudent(studentservice.findById(xsid));
		if(g == null) {
			gradeservice.save(cj);			
		}else {
			cj.setId(g.getId());
			gradeservice.save(cj);			
		}
	}
	
	@RequestMapping(value="/changegrade")
	public String changegrade(ModelMap map) {
		List<TbClass> tbClass = tbclassservice.findAll();
		map.put("tbClass", tbClass);
		return "grade/changegrade";
	}
	
	@RequestMapping(value="/stuall")
	public String stuall(Integer tbclassid,ModelMap map) {
		List<Student> students = studentservice.findByTbClassId(tbclassid);
		map.put("students", students);
		return "grade/stuall";
	}
	
	@RequestMapping(value="/student1")
	public String student1(Integer xuesheng,ModelMap map) {
		Student student = studentservice.findById(xuesheng);
		map.put("student", student);
		return "grade/change";
	}
	
	@RequestMapping(value="/updata1")
	@ResponseBody
	public void updata1(Integer grade, Integer id) {
		Grade g = gradeservice.findById(id);
		g.setGrade(grade);
		gradeservice.save(g);
	}
}
